package com.hh.servlet.showapply;

import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;

/**
 * Servlet implementation class Vcode
 */
@WebServlet("/Vcode")
public class Vcode extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Vcode() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String mobile = request.getParameter("mobile");
		String code = String.valueOf((int)(Math.random()*900000 + 100000));
		String result = SmsUtil.smsCode(mobile, code);
		Map map = JSON.parseObject(result);  
		String rCode = map.get("return_code").toString();
		System.err.println("rCode"+rCode);
		if(rCode.equals("00000")) {
			request.getSession().setAttribute(mobile, code);	
			response.getWriter().print("success");
		}else {
			response.getWriter().print("fail");
		}
	}

}
